export default {
  bind(el, binding, vnode, oldNode) {
    console.log("aaaaaaaaaa", el, binding, vnode, oldNode)
    console.dir(el)
  },
  inserted(el, ...args) {
    // 水印 指令
    const defaultSettings = {
      watermark_txt: "神笔马良",
      watermark_x: 20, // 水印起始位置x轴坐标
      watermark_y: 20, // 水印起始位置Y轴坐标
      watermark_rows: 10, // 水印行数
      watermark_cols: 30, // 水印列数
      watermark_x_space: 150, // 水印x轴间隔
      watermark_y_space: 40, // 水印y轴间隔
      watermark_color: "rgba(170, 170, 170,0.3)", // 水印字体颜色
      watermark_alpha: 1, // 水印透明度
      watermark_fontsize: "16px", // 水印字体大小
      watermark_font: "微软雅黑", // 水印字体
      watermark_width: 180, // 水印宽度
      watermark_height: 80, // 水印长度
      watermark_angle: 15 // 水印倾斜度数
    };
    // 采用配置项替换默认值，作用类似jquery.extend

    if (args.length === 1 && typeof args[0] === "object") {
      const src = args[0] || {};

      Object.keys(src).forEach((key) => {
        if (
          src[key]
          && defaultSettings[key]
          && src[key] === defaultSettings[key]
        ) { // no-empty
        } else if (src[key]) {
          defaultSettings[key] = src[key];
        }
      });
    }

    const oTemp = document.createDocumentFragment();
    // 获取页面最大宽度

    let pageWidth = Math.max(el.scrollWidth, el.clientWidth);
    const cutWidth = pageWidth * 0.015;

    pageWidth -= cutWidth;
    // 获取页面最大高度
    const pageHeight = Math.max(el.scrollHeight, el.clientHeight) + 450;
    // let pageHeight = document.body.scrollHeight+document.body.scrollTop;
    // 如果将水印列数设置为0，或水印列数设置过大，超过页面最大宽度，则重新计算水印列数和水印x轴间隔

    if (
      defaultSettings.watermark_cols == 0
      || parseInt(
        defaultSettings.watermark_x
      + defaultSettings.watermark_width * defaultSettings.watermark_cols
      + defaultSettings.watermark_x_space
      * (defaultSettings.watermark_cols - 1),
        0
      ) > pageWidth
    ) {
      defaultSettings.watermark_cols = parseInt(
        (pageWidth
          - defaultSettings.watermark_x
          + defaultSettings.watermark_x_space)
        / (defaultSettings.watermark_width + defaultSettings.watermark_x_space),
        0
      );
      defaultSettings.watermark_x_space = parseInt(
        (pageWidth
          - defaultSettings.watermark_x
          - defaultSettings.watermark_width * defaultSettings.watermark_cols)
        / (defaultSettings.watermark_cols - 1), 0
      );
    }
    // 如果将水印行数设置为0，或水印行数设置过大，超过页面最大长度，则重新计算水印行数和水印y轴间隔
    if (
      defaultSettings.watermark_rows == 0
      || parseInt(
        defaultSettings.watermark_y
      + defaultSettings.watermark_height * defaultSettings.watermark_rows
      + defaultSettings.watermark_y_space
      * (defaultSettings.watermark_rows - 1), 0
      ) > pageHeight
    ) {
      defaultSettings.watermark_rows = parseInt(
        (defaultSettings.watermark_y_space
          + pageHeight
          - defaultSettings.watermark_y)
        / (defaultSettings.watermark_height + defaultSettings.watermark_y_space), 0
      );
      defaultSettings.watermark_y_space = parseInt(
        (pageHeight
          - defaultSettings.watermark_y
          - defaultSettings.watermark_height * defaultSettings.watermark_rows)
        / (defaultSettings.watermark_rows - 1), 0
      );
    }
    let x;

    let y;

    for (let i = 0; i < defaultSettings.watermark_rows; i += 1) {
      y = defaultSettings.watermark_y
        + (defaultSettings.watermark_y_space + defaultSettings.watermark_height)
        * i;
      for (let j = 0; j < defaultSettings.watermark_cols; j += 1) {
        x = defaultSettings.watermark_x
          + (defaultSettings.watermark_width
            + defaultSettings.watermark_x_space)
          * j;

        const maskDiv = document.createElement("div");

        maskDiv.id = `maskDiv${i}${j}`;
        maskDiv.className = "maskDiv";
        maskDiv.appendChild(
          document.createTextNode(defaultSettings.watermark_txt)
        );
        // 设置水印div倾斜显示
        maskDiv.style.webkitTransform = `rotate(-${defaultSettings.watermark_angle}deg)`;
        maskDiv.style.MozTransform = `rotate(-${defaultSettings.watermark_angle}deg)`;
        maskDiv.style.msTransform = `rotate(-${defaultSettings.watermark_angle}deg)`;
        maskDiv.style.OTransform = `rotate(-${defaultSettings.watermark_angle}deg)`;
        maskDiv.style.transform = `rotate(-${defaultSettings.watermark_angle}deg)`;
        maskDiv.style.visibility = "";
        maskDiv.style.position = "absolute";
        maskDiv.style.left = `${x}px`;
        maskDiv.style.top = `${y}px`;
        maskDiv.style.overflow = "hidden";
        maskDiv.style.zIndex = "9999";
        maskDiv.style.pointerEvents = "none"; // pointer-events:none  让水印不遮挡页面的点击事件
        // maskDiv.style.border="solid #eee 1px";
        maskDiv.style.opacity = defaultSettings.watermark_alpha;
        maskDiv.style.fontSize = defaultSettings.watermark_fontsize;
        maskDiv.style.fontFamily = defaultSettings.watermark_font;
        maskDiv.style.color = defaultSettings.watermark_color;
        maskDiv.style.textAlign = "center";
        maskDiv.style.width = `${defaultSettings.watermark_width}px`;
        maskDiv.style.height = `${defaultSettings.watermark_height}px`;
        maskDiv.style.display = "block";
        maskDiv.style.fontWeight = 100;
        oTemp.appendChild(maskDiv);
      }
    }
    el.appendChild(oTemp);
  }
};
